﻿package com.yy.manager {
    import com.greensock.TweenLite;
    import com.yy.base.App;
    import com.yy.ui.core.UIComponent;
    import com.yy.ui.events.PopUpEvent;
    
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.utils.Dictionary;
    
	/**
	 *弹框管理器 
	 * @author YY
	 * 
	 */
    public class PopUpManager {

        private var _stage:Stage;
        private var _parent:DisplayObjectContainer;//容器
        public var oldWindowVisible:Boolean = false;//是否显示原来窗口
        private var modalSprite:Sprite;//模态窗口背景
        protected var windowModalDic:Dictionary;//窗口模态属性 集合
        protected var windowCenterDic:Dictionary;//窗口居中属性集合
        protected var windowPriorityDic:Dictionary;//窗口优先级级集合
        private var windowList:Array;//窗口集合

        public function PopUpManager(){
            this._stage = App.appStage;
            this.modalSprite = new Sprite();
            this.modalSprite.graphics.beginFill(0, 0.5);
            this.modalSprite.graphics.drawRect(0, 0, 10, 10);
            this.modalSprite.graphics.endFill();
            this.modalSprite.width = this._stage.stageWidth;
            this.modalSprite.height = this._stage.stageHeight;
            this.windowModalDic = new Dictionary();
            this.windowCenterDic = new Dictionary();
            this.windowPriorityDic = new Dictionary();
            this.windowList = [];
            this._stage.addEventListener(Event.RESIZE, this.resizeHandler);
        }
        public function get parent():DisplayObjectContainer{
			if(this._parent) {
				return this._parent;
			}else {
				this._stage;
			}
            return (((this._parent) || (this._stage)));
        }
        public function set parent(parent:DisplayObjectContainer):void{
            this._parent = parent;
        }
        private function resizeHandler(e:Event):void{
            this._resizeHandler(this._stage.stageWidth, this._stage.stageHeight);
        }
		
		/**
		 *舞台大小改变 
		 * @param _arg1
		 * @param _arg2
		 * 
		 */
        protected function _resizeHandler(w:Number, h:Number):void{
            var dobj:DisplayObject = this.getWindow();
            if (dobj != null){
				//是否要居中显示
                if (this.windowCenterDic[dobj] == true){
					dobj.x = ((w - dobj.width) / 2);
					dobj.y = ((h - dobj.height) / 2);
                };
            };
            this.modalSprite.width = this._stage.stageWidth;
            this.modalSprite.height = this._stage.stageHeight;
        }
		/**
		 *获得顶层窗口 
		 * @return 
		 * 
		 */
        public function getWindow():DisplayObject{
            if (this.windowList.length > 0){
                return (this.windowList[(this.windowList.length - 1)]);
            };
            return (null);
        }
		/**
		 *获得弹框列表 
		 * @return 
		 * 
		 */
        public function getWindowList():Array{
            return (this.windowList);
        }
		
		/**
		 *添加弹框 
		 * @param window  弹框内容
		 * @param modal	是否是模态窗口
		 * @param alignCenter	是否居中对齐
		 * @param priority	优先级
		 * 
		 */
        public function addPopUp(window:DisplayObject, modal:Boolean=false, alignCenter:Boolean=true, priority:int=1):void{
            var i:int;
			var len:int;
            var dobj:DisplayObject;
            this.windowModalDic[window] = modal;
            this.windowCenterDic[window] = alignCenter;
            this.windowPriorityDic[window] = priority;
			//根据优先级添加弹框到弹框列表
            if (this.windowList.length > 0){
                i = 0;
				len = windowList.length;
				for(i=0; i<len; i++) {
					dobj = this.windowList[i];
					if (this.windowPriorityDic[window] < this.windowPriorityDic[dobj]){
						this.windowList.splice(i, 0, window);
						break;
					};
					//如果找不到优先级比要添加的弹框低的弹框 则直接添加到弹框列表
					if (i == (this.windowList.length - 1)){
						this.windowList.push(window);
						break;
					};
				}
            } else {
                this.windowList.push(window);
            };
			//是否显示原来的弹框
            if (!this.oldWindowVisible){
				len = this.windowList.length;
				for(i=0; i<len; i++) {
					DisplayObject(this.windowList[i]).visible = false;
				}
            };
			//判断 弹框列表最后一个弹框是否是 当前添加的弹框
            if (this.windowList.length > 0 && this.windowList[(this.windowList.length - 1)] != window){
            } else {
                this._addPopUp(window);
            };
        }
		/**
		 *创建一个窗口（不常用） 
		 * @param cls	窗口类
		 * @param modal	模态属性
		 * @param alignCenter	居中属性
		 * @param data	传给窗口的数据
		 * @param priority 优先级
		 * @return 
		 * 
		 */
        public function createPopUp(cls:Class, modal:Boolean=false, alignCenter:Boolean=true, data:Object=null, priority:Number=0):UIComponent{
            var window:UIComponent = new cls() as UIComponent;
            if (window == null){
                throw (new Error("必须是UIComponent或其子类"));
            };
            if (window.hasOwnProperty("data")){
				window["data"] = data;
            };
            this.addPopUp(window, modal, alignCenter, priority);
            return window;
        }
		/**
		 *添加到舞台 
		 * @param window
		 * 
		 */
        private function _addPopUp(window:DisplayObject):void{
			window.visible = true;
            if (this.windowModalDic[window]){
                this.parent.addChild(this.modalSprite);
            };
            this.addPopUpToStage(window, this.windowCenterDic[window]);
        }
		/**
		 *显示弹框到舞台 层
		 * @param window
		 * @param alignCenter
		 * 
		 */
        protected function addPopUpToStage(window:DisplayObject, alignCenter:Boolean):void{
            var offsetX:Number;
            var offsetY:Number;
            var initScale:Number;
            this.parent.addChild(window);
            if (alignCenter){
				initScale = 0.5;
				offsetX = ((this._stage.stageWidth - window.width) / 2);
				offsetY = ((this._stage.stageHeight - window.height) / 2);
				window.x = ((this._stage.stageWidth - (window.width * initScale)) / 2);
				window.y = ((this._stage.stageHeight - (window.height * initScale)) / 2);
				window.scaleX = initScale;
				window.scaleY = initScale;
				window.alpha = 0.5;
                TweenLite.to(window, 0.1, {
                    x:offsetX,
                    y:offsetY,
                    scaleX:1,
                    scaleY:1,
                    alpha:1,
                    onComplete:this.popUpComplete
                });
            };
        }
        private function popUpComplete():void{
            trace("弹框缓动完成！");
        }
        public function removePopUp(window:DisplayObject):void{
            if (window && this.parent.contains(window)){
                this.removePopUpToStage(window);
            };
            if (this.parent.contains(this.modalSprite)){
                this.parent.removeChild(this.modalSprite);
            };
            delete this.windowModalDic[window];
            delete this.windowCenterDic[window];
            delete this.windowPriorityDic[window];
            var index:int = this.windowList.indexOf(window);
            if (index != -1){
                this.windowList.splice(index, 1);
            };
            if (this.windowList.length > 0 && index == this.windowList.length){
                this._addPopUp((this.windowList[(this.windowList.length - 1)] as DisplayObject));
            };
        }
        protected function removePopUpToStage(window:DisplayObject):void{
            this.parent.removeChild(window);
        }

    }
}